package com.zjw.xm.xmuser.controller;


import com.zjw.xm.entity.CrBought;

import com.zjw.xm.entity.CrCart;
import com.zjw.xm.entity.CrUser;
import com.zjw.xm.xmuser.bean.Result;
import com.zjw.xm.xmuser.mapper.CrBoughtMapper;

import com.zjw.xm.xmuser.service.impl.CrBoughtServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;


import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @program: c116-s3-xm
 * @description:
 * @author: 作者
 * @create: 2022-08-16 10:58
 */
@RestController
@RequestMapping("/xm/crBought")
public class CrBoughtController {
    @Resource
    CrBoughtMapper crBoughtMapper;
    @Resource
    CrBoughtServiceImpl crBoughtService;


    //添加订单并且清空购物车
    @GetMapping("addOrder")
    public Result addOrder(@SessionAttribute Map<String, Object> loginedUser) {
        int id = Integer.valueOf(loginedUser.get("id") + "");
        List<CrBought> crBoughts = crBoughtMapper.selectCart(id);
        List<Integer> crBoughtsID = crBoughtMapper.selectCartId(id);
        crBoughtService.saveBatch(crBoughts);
        for (int i = 0; i < crBoughtsID.size(); i++) {
            int myuid = crBoughtsID.get(i);
            crBoughtMapper.deleteByCid(myuid);
        }
        return Result.success("添加订单成功", null);
    }

}